package com.net.request

import com.net.NetUtils
import okhttp3.Headers

/**
 * @describe:
 * @Author: lixiaopeng
 * @Date: 2020/6/28
 */
abstract class BaseRequestBuilder {
    protected var url = ""
    protected var params: MutableMap<String, String>? = null
    protected var headers: MutableMap<String, String>? = null
    protected var tag: Any? = null

    fun tag(tag: Any): BaseRequestBuilder {
        this.tag = tag
        return this
    }

    fun url(url: String): BaseRequestBuilder {
        this.url = url
        return this
    }

    fun addParams(params: MutableMap<String, String>): BaseRequestBuilder {
        this.params = params
        return this
    }

    fun addParam(key: String, value: String): BaseRequestBuilder {
        if (this.params == null) {
            this.params = mutableMapOf()
        }
        this.params?.put(key, value)
        return this
    }

    fun addHeader(key: String, value: String): BaseRequestBuilder {
        if (this.headers == null) {
            this.headers = mutableMapOf()
        }
        this.headers?.put(key, value)
        return this
    }

    fun addHeader(headers: MutableMap<String, String>): BaseRequestBuilder {
        this.headers = headers
        return this
    }

    /**
     * 构建header
     */
    protected fun generateHeader(headers: MutableMap<String, String>): Headers {
        val builder = Headers.Builder()
        headers.forEach { (key, value) ->
            builder.add(key, value)
        }
        return builder.build()
    }


    abstract fun build(): NetUtils
}

